#pragma once
#include "logger.hpp"
#include <mysql/mysql.h>
#include <string>
#include <iostream>
#include <sstream> 
#include <memory>
#include <fstream>
#include <jsoncpp/json/json.h>

class mysql_util
{
public:
    static MYSQL *mysql_create(const std::string &host, const std::string &username, const std::string &password, const std::string &dbname, uint16_t port = 3306)
    {
        MYSQL *mysql = mysql_init(nullptr);
        if (mysql == nullptr)
        {
            ERR_LOG("mysql init error...");
            return nullptr;
        }
        // 2.连接数据库
        if (mysql_real_connect(mysql, host.c_str(), username.c_str(), password.c_str(), dbname.c_str(), port, NULL, 0) == nullptr)
        {
            ERR_LOG("connect mysql server error: %s", mysql_error(mysql));
            mysql_close(mysql);
            return nullptr;
        }
        // 3.设置客户端字符集
        if (mysql_set_character_set(mysql, "utf8") != 0)
        {
            ERR_LOG("set mysql character error: %s", mysql_error(mysql));
            mysql_close(mysql);
            return nullptr;
        }
        return mysql;
    }
    static bool mysql_exec(MYSQL *mysql, const std::string &sql)
    {
        int ret = mysql_query(mysql, sql.c_str());
        if (ret)
        {
            ERR_LOG("%s", sql.c_str());
            ERR_LOG("mysql execute error: %s", mysql_error(mysql));
            // mysql_close(mysql);
            return false;
        }
        return true;
    }
    static void mysql_destory(MYSQL *mysql)
    {
        if (mysql != nullptr)
        {
            mysql_close(mysql);
        }
        return;
    }
};

class json_util
{
public:
    static bool Serialize(const Json::Value &root, std::string &str)
    {
        Json::StreamWriterBuilder swb;                                 // 工厂类对象
        std::unique_ptr<Json::StreamWriter> sw(swb.newStreamWriter()); // 使用工厂类创建对象
        std::stringstream ss;
        int ret = sw->write(root, &ss);
        if (ret != 0)
        {
            ERR_LOG("Serialize failed!");
            return false;
        }
        str = ss.str();
        return true;
    }
    static bool UnSerialize(const std::string &str, Json::Value &root)
    {
        Json::CharReaderBuilder crb;
        std::unique_ptr<Json::CharReader> cr(crb.newCharReader());
        std::string err;
        bool ret = cr->parse(str.c_str(), str.c_str() + str.size(), &root, &err);
        if (!ret)
        {
            ERR_LOG("UnSerialize failed : %s", err.c_str());
            return false;
        }
        return true;
    }
};

class string_util
{
public:
    static int split(const std::string &src, const std::string &sep, std::vector<std::string> &res)
    {
        size_t pos, idx = 0;
        while (idx < src.size())
        {
            pos = src.find(sep, idx);
            if (pos == std::string::npos)
            {
                res.push_back(src.substr(idx));
                break;
            }
            if(pos == idx)//相等的意思是遇到了相同的sep，123,456,,,789，后面处理会push_back空字符串
            {
                idx += sep.size();
                continue;
            }
            res.push_back(src.substr(idx, pos - idx)); // 截取pos-idx的长度
            idx = pos + sep.size();
        }
        return res.size();
    }
};

class file_util
{
public:
    static bool read(const std::string &filename,std::string &body)
    {
        //1.打开文件
        std::ifstream ifs(filename,std::ios::binary);//以二进制方式打开文件
        if(!ifs.is_open())
        {
            ERR_LOG("打开 %s 文件失败",filename.c_str());
            return false;
        }
        //2.算出文件大小
        ssize_t filesize = 0;
        ifs.seekg(0,std::ios::end);//相当于是光标移动到文件末尾
        filesize = ifs.tellg();//算出当前位置与开头位置的偏移量
        ifs.seekg(0,std::ios::beg);//再移回去
        body.resize(filesize);
        //3.将文件内容取出
        ifs.read(&body[0],filesize);
        if(!ifs.good())
        {
            ERR_LOG("读取 %s 文件失败",filename.c_str());
            ifs.close();
            return false;
        }
        //4.关闭文件
        ifs.close();
        return true;
    }
};